package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.GoodsVo;
import com.cskaoyan.mapper.MarketGoodsAttributeMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.util.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    @Override
    public BaseRespVo create(GoodsVo goodsVo) {
        // 获取参数
        MarketGoods goods = goodsVo.getGoods();
        MarketGoodsAttribute[] goodsAttributes = goodsVo.getAttributes();
        MarketGoodsProduct[] goodsProducts = goodsVo.getProducts();
        MarketGoodsSpecification[] goodsSpecifications = goodsVo.getSpecifications();

        try {
            // 获取mappers
            SqlSession sqlSession = TransactionUtil.getSqlSession();
            MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
            MarketGoodsAttributeMapper goodsAttributeMapper = sqlSession.getMapper(MarketGoodsAttributeMapper.class);
            MarketGoodsProductMapper goodsProductMapper = sqlSession.getMapper(MarketGoodsProductMapper.class);
            MarketGoodsSpecificationMapper goodsSpecificationMapper = sqlSession.getMapper(MarketGoodsSpecificationMapper.class);

            // 获取现在的时间
            Date date = new Date();
            // // 验证参数
            // BaseRespVo error = validate(goodsVo);
            // if (error != null) {
            //     return error;
            // }

            // 排除同名商品
            String name = goods.getName();
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria marketGoodsExampleCriteria = marketGoodsExample.createCriteria();
            marketGoodsExampleCriteria.andNameEqualTo(name);
            List<MarketGoods> olderGoods = goodsMapper.selectByExample(marketGoodsExample);
            if (olderGoods.size() > 0) {
                return BaseRespVo.fail(611, "商品名已经存在");
            }

            // 商品表里面有一个字段retailPrice记录当前商品的最低价
            BigDecimal retailPrice = new BigDecimal(Integer.MAX_VALUE);
            for (MarketGoodsProduct product : goodsProducts) {
                BigDecimal productPrice = product.getPrice();
                if (retailPrice.compareTo(productPrice) == 1) {
                    retailPrice = productPrice;
                }
            }
            goods.setRetailPrice(retailPrice);

            //  商品基本信息表market_goods
            goods.setAddTime(date);
            goods.setUpdateTime(date);
            goodsMapper.insert(goods);
            goods = goodsMapper.selectByExample(marketGoodsExample).get(0);

            // 商品规格表market_goods_specification
            for (MarketGoodsSpecification specification : goodsSpecifications) {
                specification.setGoodsId(goods.getId());
                specification.setId(null);
                specification.setAddTime(date);
                specification.setUpdateTime(date);
                goodsSpecificationMapper.insert(specification);
            }

            // 商品参数表market_goods_attribute
            for (MarketGoodsAttribute attribute : goodsAttributes) {
                attribute.setGoodsId(goods.getId());
                attribute.setId(null);
                attribute.setAddTime(date);
                attribute.setUpdateTime(date);
                goodsAttributeMapper.insert(attribute);
            }

            // 商品货品表market_product
            for (MarketGoodsProduct product : goodsProducts) {
                product.setId(null);
                product.setGoodsId(goods.getId());
                product.setAddTime(date);
                product.setUpdateTime(date);
                goodsProductMapper.insert(product);
            }
        }catch (Exception exception) {
            exception.printStackTrace();
            TransactionUtil.rollback();
        }
        TransactionUtil.commit();

        // goodsVo.setGoods(goods);
        // goodsVo.setAttributes(goodsAttributes);
        // goodsVo.setProducts(goodsProducts);
        // goodsVo.setSpecifications(goodsSpecifications);
        return BaseRespVo.ok();
    }

    // private BaseRespVo validate(GoodsVo goodsVo) {
    //     MarketGoods goods = goodsVo.getGoods();
    //     MarketGoodsProduct[] goodsProducts = goodsVo.getProducts();
    //
    //     return null;//todo
    //
    //
    // }
    //
    // public boolean isInteger(String str) {
    //     return str.matches("^\\d+$");
    // }
}
